<template>
  <a-modal title='订单商品选择' width='88%' :dialog-style="{ top: '10px' }" :visible='visible' :confirmLoading='loading' :footer='null' @cancel='handleCancel'>
    <f-search-form :more='false' :showRestBtn='false' @searchHandler='searchHandler'>
      <f-search-form-item label='商品名称' name='title' style='width: 350px' />
    </f-search-form>
    <f-table ref='table' rowKey='drugNo' :scroll='{x:800,y:150}' v-if='visible' @rowClick='drugClick' :params='queryParam' :tableColumns='columns' :multiple='false' :showPagination='false' :api='api.bpd.selectTop10HyDrugList'>
<!--      <span slot='action' slot-scope='text, record'>
         <template>
          <a @click='updateDrugOnUseCount(record)'>计算</a>
        </template>
      </span>-->
    </f-table>
    <a-divider orientation='left'>商品批号</a-divider>
    <f-table ref='lotTable' rowKey='drugsLotId' :rowSelection='rowSelection' :scroll='{x:true}' @dblclick='dblclick' v-if='visible' :tableColumns='logColumns' :multiple='false' :showRadio='true' :params='queryParam' :api='api.bpd.selectLotNoByDrugsId' :autoLoad='false' :showPagination='false' @onSelectChange='onSelectChange'>

    </f-table>
  </a-modal>
</template>
<script>
import api from "../js/api"

export default {
  name: "SelectHyDrugsModal",
  props: {},
  data() {
    return {
      api: Object.assign(api, this.api),
      refs: this.$refs,
      loading: false,
      visible: false,
      selectedRowKeys: [],
      selectedRows: [],
      //列表初始化查询参数
      queryParam: {
        hyDeptNo: null
      },
      drugSelectRow: {},
      // 表头
      columns: [
        {
          title: "商品名称",
          align: "left",
          width: "160px",
          ellipsis: true,
          dataIndex: "drugsName"
        },
        {
          title: "规格",
          align: "left",
          width: "130px",
          ellipsis: true,
          dataIndex: "drugsSpec"
        },
        {
          title: "可开票总数",
          align: "center",
          width: "68px",
          dataIndex: "surplusNum"
        },
        {
          title: "在单总数",
          align: "center",
          width: "60px",
          dataIndex: "onUseCount"
        },
        {
          title: "锁定数",
          align: "center",
          width: "60px",
          dataIndex: "lockCount"
        },
        {
          title: "件容",
          align: "center",
          width: "50px",
          dataIndex: "drugJr"
        },
        {
          title: "单位",
          align: "center",
          width: "40px",
          dataIndex: "drugsUnit"
        },
        {
          title: "生产企业",
          align: "left",
          width: "120px",
          ellipsis: true,
          dataIndex: "producersName"
        },
        {
          title: "供货商",
          align: "left",
          width: "100px",
          ellipsis: true,
          dataIndex: "supplierName"
        },
        {
          title: "上市许可",
          align: "left",
          width: "100px",
          ellipsis: true,
          dataIndex: "cyr"
        },
       /* {
          title: "操作",
          align: "center",
          width: "100px",
          hidden:!this.superAdmin,
          dataIndex: "action",
          scopedSlots: { customRender: "action" }
        }*/
      ],
      // 表头
      logColumns: []
    }
  },
  computed: {
    rowSelection() {
      return {
        type: "radio",
        getCheckboxProps: record => ({
          props: {
            disabled: record.canUseNum <= 0 || record.onUseCount < 0
          }
        })
      }
    }
  },
  methods: {
    open(hyDeptNo, deptType) {
      this.queryParam.hyDeptNo = hyDeptNo
      this.visible = true
      this.selectedRowKeys = []
      this.selectedRows = []
      this.getLogColumns(deptType)
    },
    close() {
      this.$emit("close")
      this.visible = false
    },
    drugClick(record, event) {
      let drugNo = record.drugNo
      this.queryParam.drugNo = drugNo
      this.drugSelectRow = record
      this.$refs.lotTable.refresh()
    },
    getLogColumns(deptType) {
      let hideCostPrice = deptType == "ZY"?true:false
      this.logColumns = [
        {
          title: "可开票数",
          align: "center",
          width: "70px",
          dataIndex: "canUseNum"
        },
        {
          title: "库存数量",
          align: "center",
          width: "60px",
          dataIndex: "surplusNum"
        },
        {
          title: "在单数",
          align: "center",
          width: "60px",
          dataIndex: "localOnUserCount"
        },
        {
          title: "锁定数",
          align: "center",
          width: "80px",
          dataIndex: "lockCount"
        },
        {
          title: "仓库名称",
          align: "left",
          width: "120px",
          dataIndex: "storeName"
        },
        {
          title: "批号",
          align: "center",
          width: "70px",
          dataIndex: "drugLotNo"
        },
        {
          title: "件容",
          align: "center",
          width: "60px",
          dataIndex: "drugJr"
        },
        {
          title: "整件数",
          align: "center",
          width: "80px",
          dataIndex: "wholeNum"
        },
        {
          title: "零货数",
          align: "center",
          width: "70px",
          dataIndex: "partNum"
        },
        {
          title: "成本价",
          align: "center",
          width: "70px",
          hidden: hideCostPrice,
          dataIndex: "costPrice"
        },
        {
          title: "入库数量",
          align: "center",
          width: "80px",
          dataIndex: "inStoreNum"
        },
        {
          title: "出库数量",
          align: "center",
          width: "80px",
          dataIndex: "saleNum"
        },
        {
          title: "入库日期",
          align: "center",
          width: "80px",
          dataIndex: "inStoreDate"
        },
        {
          title: "有效期",
          align: "center",
          width: "80px",
          customCell: this.renderTdBackground,
          dataIndex: "expirationDate"
        },
        {
          title: "生产日期",
          align: "center",
          width: "80px",
          dataIndex: "generationDate"
        }
      ]
    },
    dblclick(record, event) {
      let canUseNum = record.canUseNum
      let onUseCount = record.onUseCount
      if (onUseCount < 0) {
        this.$message.success("在单数异常")
        if (this.superAdmin) {
          this.updateOnUseCount(record)
        }
        return
      }
      if (canUseNum <= 0) {
        this.$message.success("库存不足")
        return
      }
      this.$emit("drugsSelectChange", record, this.drugSelectRow)
      this.close()
    },
    handleCancel() {
      this.close()
    },
    updateDrugOnUseCount(record) {
      this.http.post(this, {
        url: this.api.bpd.updateDrugOnUseCount,
        data: {
          drugNo: record.drugNo
        },
        success: (me, data) => {
          this.$refs.table.refresh()
        }
      })
    },
    updateOnUseCount(record) {
      this.http.post(this, {
        url: this.api.bpd.updateOnUseCount,
        data: {
          drugLotId: record.drugLotId,
          drugNo: record.drugNo,
          drugLotNo: record.drugLotNo,
          storeCode: record.storeCode
        },
        success: (me, data) => {
          this.$refs.lotTable.refresh()
        }
      })
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    renderTdBackground(record, flag) {
      if (record.isJsq==1) {
        return {
          style: {
            "background-color": "yellow",
          }
        }
      }
    },
  },
  created() {
  },
  components: {}
}
</script>

<style scoped lang='less' rel='stylesheet/less'>

</style>
